package com.ssm.springboot.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.springboot.dao.TbRoleDao;
import com.ssm.springboot.dao.TbRolePermissionDao;
import com.ssm.springboot.dao.TbUserRoleDao;
import com.ssm.springboot.pojo.DataInfo;
import com.ssm.springboot.pojo.TbRole;
import com.ssm.springboot.pojo.req.UpdateRoleRes;
import com.ssm.springboot.pojo.req.UpdateUserRoleRes;
import com.ssm.springboot.pojo.resp.PermissionRoleResp;
import com.ssm.springboot.pojo.resp.TbPermissionResp;
import com.ssm.springboot.pojo.resp.UserRoleResp;
import com.ssm.springboot.service.TbRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (TbRole)表服务实现类
 *
 * @author makejava
 * @since 2021-12-27 22:29:21
 */
@Service
public class TbRoleServiceImpl implements TbRoleService {
    @Resource
    private TbRoleDao tbRoleDao;

    @Autowired
    private TbRolePermissionDao tbRolePermissionDao;

    @Autowired
    private TbUserRoleDao tbUserRole;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TbRole queryById(Integer id) {
        return this.tbRoleDao.queryById(id);
    }

    @Override
    public DataInfo findByRoleid(int roleid) {
        List<PermissionRoleResp> byRoleid = tbRoleDao.findByRoleid(roleid);
        List perlist = new ArrayList();
        for (PermissionRoleResp pj : byRoleid) {
            TbPermissionResp tbPermissionResp = new TbPermissionResp();
            tbPermissionResp.setId(pj.getId());
            tbPermissionResp.setTitle(pj.getPermissionName());
            if (pj.getRoleid() != null) {
                tbPermissionResp.setChecked(true);
            }
            perlist.add(tbPermissionResp);
        }
        return new DataInfo(0, null, null, perlist);
    }

    /**
     * 查询多条数据
     *
     * @param page  当前页数
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public DataInfo queryAllByLimit(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<TbRole> tbRoles = tbRoleDao.queryAllByLimit();
        PageInfo<TbRole> of = PageInfo.of(tbRoles);
        return new DataInfo(0, null, of.getTotal(), of.getList());
    }



    /**
     * 修改数据
     *
     * @param tbRole 实例对象
     * @return 实例对象
     */
    @Override
    public TbRole update(TbRole tbRole) {
        this.tbRoleDao.update(tbRole);
        return this.queryById(tbRole.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tbRoleDao.deleteById(id) > 0;
    }

    //更新角色权限
    @Override
    @Transactional
    public DataInfo tbRoleService(UpdateRoleRes updateRoleRes) {
        //通过角色id删除中间表
        tbRolePermissionDao.deleteByRoleid(updateRoleRes.getRoleid());
        List<PermissionRoleResp> perlist = updateRoleRes.getPerlist();
        if (perlist.size() > 0) {
            for (PermissionRoleResp p : perlist) {
                tbRolePermissionDao.insert(updateRoleRes.getRoleid(), p.getId());
            }
            return new DataInfo(0, "更新成功", null, null);
        }
        return new DataInfo(0, "更新成功", null, null);
    }

    //查询用户角色
    @Override
    public DataInfo findByUserid(Integer userid) {

        List list = new ArrayList();
        List<UserRoleResp> byUserid = tbRoleDao.findByUserid(userid);
        for (UserRoleResp userRoleResp : byUserid) {
            TbPermissionResp tbPermissionResp = new TbPermissionResp();
            tbPermissionResp.setId(userRoleResp.getId());
            tbPermissionResp.setTitle(userRoleResp.getRoleName());
            if (userRoleResp.getUserId() != null) {
                tbPermissionResp.setChecked(true);
            }
            list.add(tbPermissionResp);
        }
        return new DataInfo(0, "查询成功", null, list);
    }

    //更新用户角色
    @Override
    @Transactional
    public DataInfo updateUserRole(UpdateUserRoleRes updateRoleRes) {
        tbUserRole.deleteByUserid(updateRoleRes.getUserid());
        List<UserRoleResp> perlist = updateRoleRes.getPerlist();
        if(perlist.size()>0){
            for(UserRoleResp u:perlist){
                tbUserRole.add(updateRoleRes.getUserid(),u.getId());
            }
        }
        return new DataInfo(0,"修改完成",null,null);
    }
//添加角色
    @Override
    public DataInfo add(TbRole tbRole) {
        tbRoleDao.update(tbRole);
        return new DataInfo(0,"增加成功",null,null);
    }


}